package com.dzm.xgb.core.controller.api;


import com.dzm.common.result.R;
import com.dzm.common.util.JwtUtils;
import com.dzm.xgb.core.hfb.RequestHelper;
import com.dzm.xgb.core.pojo.entity.LendReturn;
import com.dzm.xgb.core.service.LendReturnService;
import com.dzm.xgb.core.service.LendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 还款记录表 前端控制器
 * </p>
 *
 * @author Dzm
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/api/core/lendReturn")
public class LendReturnController {

    @Autowired
    private LendReturnService lendReturnService;

    @GetMapping("/list/{id}")
    public R list(@PathVariable("id") Long id) {
        List<LendReturn> list = lendReturnService.getListByLendId(id);

        return R.ok().data("list", list);
    }

    @PostMapping("/auth/commitReturn/{lendReturnId}")
    public R commitReturn(@PathVariable("lendReturnId")Long lendReturnId, @RequestHeader("token") String token) {
        String formStr = lendReturnService.commitReturn(lendReturnId, JwtUtils.getUserId(token));

        return R.ok().data("formStr", formStr);
    }

    @PostMapping("/notifyUrl")
    public String notifyBorrowerReturn(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = RequestHelper.switchMap(parameterMap);
        //验签
        if(RequestHelper.isSignEquals(map)) {
            if("0001".equals(map.get("resultCode"))) {
                return lendReturnService.notifyBorrowerReturn(map);
            }
            return "fail";
        }

        return "非法访问！";
    }
}

